package com.wuyou.simple;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * 面试题 01.02. 判定是否互为字符重排
 */
public class CheckPermutation {

    public static void main(String[] args) {
        CheckPermutation checkPermutation = new CheckPermutation();
        String s1 ="abc";
        String s2 = "bad";
        System.out.println(checkPermutation.solution_3(s1, s2));
        Integer.parseInt(s1, 2);
    }

    public boolean solution_3(String s1, String s2){
        if (s1.length() != s2.length()) return false;
        int[] check = new int[26];
        Arrays.fill(check, 0);
        for (int i = 0; i < s1.length(); i++) {
            int index = s1.charAt(i) - 'a';
            check[index] += 1;
            int index1 = s2.charAt(i) - 'a';
            check[index1] -= 1;

        }

        for (int i = 0; i < check.length; i++) {
            if(check[i] != 0) return false;
        }
        return true;
    }

    public boolean solution_2(String s1, String s2){
        if (s1.length() != s2.length()) return false;
        char[] chars = s1.toCharArray();
        char[] chars1 = s2.toCharArray();
        Arrays.sort(chars);
        Arrays.sort(chars1);
        for (int i = 0; i < chars.length; i++) {
            if (chars[i] != chars1[i]) {
                return false;
            }
        }
        return true;
    }
    public boolean solution_1(String s1, String s2) {
        if (s1.length() != s2.length()) return false;
        char[] chars = s1.toCharArray();
        char[] chars1 = s2.toCharArray();
        Arrays.sort(chars);
        Arrays.sort(chars1);
        Map<Character, Integer> map1 = new HashMap<>();
        Map<Character, Integer> map2 = new HashMap<>();
        for (int i = 0; i < chars.length; i++) {
            map1.put(chars[i], map1.getOrDefault(chars[i], 0) + 1);
            map2.put(chars1[i], map2.getOrDefault(chars1[i], 0) + 1);
            if (chars[i] != chars1[i] || map1.get(chars[i]) != map2.get(chars1[i])) {
                return false;
            }
        }
        return true;
    }
}
